home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Development Libraries / SGI IRIX 6.4 Development Libraries.iso / docs6.4 / relnotes / dmedia_dev / ch08.z / ch08
Encoding:
Text File  |  1997-09-04  |  94.4 KB  |  1,717 lines

  1.  
  2.  
  3.  
  4.                                   - 1 -
  5.  
  6.  
  7.  
  8.        9.  _A_u_d_i_o__F_i_l_e__L_i_b_r_a_r_y
  9.  
  10.        This chapter lists information about the Audio File Library
  11.        programming interface included in the the IRIS Digital Media
  12.        Development Environment.  It includes changes, additions,
  13.        and bug fixes since the last release, and known problems,
  14.        workarounds, and caveats.
  15.  
  16.        The Audio File Library consists of approximately 100 C
  17.        routines that allow application programs to read/write audio
  18.        sample data and auxiliary header information to/from a
  19.        number of standard audio file formats.  The library requires
  20.        no special audio hardware at runtime, and is useful for
  21.        sound file import/export/conversion operations as well as
  22.        real-time recording/playback.
  23.  
  24.        The Audio File Library API allows you to access audio
  25.        samples and nonaudio information from audio files using a
  26.        common set of routines.  The API includes functions for
  27.        reading/writing various types of data including author,
  28.        copyright, name, and annotation strings; sample frame marker
  29.        locations; sample configuration parameters and loop points;
  30.        MIDI exclusive data; and application-defined data.
  31.  
  32.        The Audio File Library implements transparent sample data
  33.        format conversion and compression/decompression for many
  34.        different audio file formats.  The following compression
  35.        algorithms are currently supported:
  36.  
  37.           +o MPEG-1 Layers I and II
  38.  
  39.           +o Aware Inc.'s, proprietary MultiRate algorithm
  40.  
  41.           +o CCITT G.711 mu-law and A-law
  42.  
  43.           +o CCITT G.722 ADPCM
  44.  
  45.           +o CCITT G.726 ADPCM
  46.  
  47.           +o CCITT G.728 ADPCM
  48.  
  49.           +o GSM 06.10
  50.  
  51.           +o IMA DVI ADPCM
  52.  
  53.        Transparent format conversion and compression/decompression
  54.        means that application programs can process all audio files
  55.        as uncompressed data in any format that the application
  56.        desires.  There is no need to write special code for
  57.        handling different sample formats or types of compressed
  58.        data.  Compressed and decompressed files look the same to
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                                   - 2 -
  71.  
  72.  
  73.  
  74.        applications as long as the compression format is one of the
  75.        supported types listed above.
  76.  
  77.        Note:  The Aware MultiRate codec cannot be activated by an
  78.               application without a FlexLM  node-locked license,
  79.               available from SGI.
  80.  
  81.               See the online man page _a_w_a_r_e(_5) for an overview of
  82.               the Aware compression technology available to
  83.               developers and end users. The online man page
  84.               _a_f_I_n_i_t_C_o_m_p_r_e_s_s_i_o_n(3dm) describes the Audio File
  85.               Library interface to reading/writing audio files that
  86.               contain MultiRate or other types of compressed data.
  87.  
  88.        The Audio File Library API is intended to be general enough
  89.        to allow support for additional digital audio file formats
  90.        and compression schemes in the future.  Applications that
  91.        are developed on top of the 5.3 library will be able to read
  92.        these additional file and data formats (with at most minor
  93.        code modifications) after simply relinking with a 6.x
  94.        version of the Audio File Library. Existing 5.3 applications
  95.        will require slight modifications in order to write
  96.        additional file and data formats.
  97.  
  98.        Applications linked with the 6.x Audio File Library no
  99.        longer need to be linked with any other library.  All that
  100.        is needed on the link line is:
  101.  
  102.        ----llllaaaauuuuddddiiiiooooffffiiiilllleeee
  103.  
  104.        The only exception to this is cases where the general
  105.        Digital Media Library error retrieval routine
  106.        _d_m_G_e_t_E_r_r_o_r(3dm) is used (see details below).
  107.  
  108.        The _I_R_I_S _D_i_g_i_t_a_l _M_e_d_i_a _P_r_o_g_r_a_m_m_i_n_g _G_u_i_d_e contains an
  109.        introduction to programming with the Audio File Library,
  110.        along with a description of each of its functions.  Please
  111.        see section "Documentation Errors" for some important notes.
  112.  
  113.        The on-line man page _a_f_I_n_t_r_o(3dm) provides a brief overview
  114.        of the library and a list of the available procedure calls.
  115.  
  116.        The IRIS Audio Utility Library is a small C language
  117.        procedure call library that contains approximately 10
  118.        auxiliary routines for use with the Audio File Library.
  119.  
  120.        Please see the _I_R_I_S _D_i_g_i_t_a_l _M_e_d_i_a _P_r_o_g_r_a_m_m_i_n_g _G_u_i_d_e for an
  121.        introduction to programming with Audio Utility Library and a
  122.        detailed description of its functions.  Also see the Changes
  123.        and Additions section below for more information about this
  124.        library.
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.                                   - 3 -
  137.  
  138.  
  139.  
  140.        Currently, the library consists of the function
  141.        _A_U_c_h_e_c_k_l_i_c_e_n_s_e(3dm), which allows an application to query
  142.        license availability for Aware compressor/decompressor
  143.        software on the current host, and several functions that
  144.        implement a generalized parameter-value list object for use
  145.        with the new Audio File Library routines
  146.        _a_f_I_n_i_t_C_o_m_p_r_e_s_s_i_o_n_P_a_r_a_m_s(3dm) and
  147.        _a_f_G_e_t_C_o_m_p_r_e_s_s_i_o_n_P_a_r_a_m_s(3dm).  See the man page _A_U_p_v_n_e_w(3dm)
  148.        for a description of these routines.
  149.  
  150.        See the source code _a_i_f_c_c_o_n_v_e_r_t._c in the directory
  151.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s for Audio Utility
  152.        programming example.
  153.  
  154.  
  155.        9.1  _E_x_a_m_p_l_e__P_r_o_g_r_a_m_s__i_n__/_u_s_r_/_s_h_a_r_e_/_s_r_c_/_d_m_e_d_i_a_/_a_u_d_i_o
  156.  
  157.        The subsystems _d_m_e_d_i_a__d_e_v._s_r_c._t_o_o_l_s and
  158.        _d_m_e_d_i_a__d_e_v._s_r_c._e_x_a_m_p_l_e_s contain several source code examples
  159.        for programming with the Audio File Library. Executable
  160.        versions of some of these programs are included as standard
  161.        system utilities which can be installed from the
  162.        _d_m_e_d_i_a__e_o_e._s_w._t_o_o_l_s subsystem included with the IRIX 6.2 O/S
  163.        release.  However, as noted in the Changes section below,
  164.        most of these programs are now obsolete and are only
  165.        provided as example code, and their functionality has been
  166.        taken over by other programs.
  167.  
  168.        _p_l_a_y_a_i_f_c(1) and _r_e_c_o_r_d_a_i_f_c(1) are command-line programs for
  169.        playing and recording AIFF-C/AIFF files, respectively.  The
  170.        programs are built on top of the IRIS Audio Library and the
  171.        Audio File Library.  The source code for these programs is
  172.        available in
  173.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s/{_p_l_a_y._c,_s_f_r_e_c_o_r_d._c}
  174.  
  175.        _a_i_f_c_i_n_f_o(1) is a command-line program that parses an AIFF-
  176.        C/AIFF input file and prints a description of the data in
  177.        the file, including characteristics of the audio sample
  178.        data, and a summary of the nonaudio data (strings, loop
  179.        points, and so on) found in the various file header fields.
  180.        The source code is installed in
  181.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s/_a_i_f_c_i_n_f_o._c.
  182.  
  183.        _a_i_f_f_2_a_i_f_c(1), _a_i_f_c_2_a_i_f_f(1), _a_i_f_c_c_o_m_p_r_e_s_s(1), and
  184.        _a_i_f_c_d_e_c_o_m_p_r_e_s_s(1) are old command-line utility programs for
  185.        converting between AIFF-C and AIFF files, or for converting
  186.        AIFF-C files between compressed and uncompressed data
  187.        formats.  These utilities are all obsolete, as indicated
  188.        below.  The source for the program is the file
  189.        /_u_s_r/_s_h_a_r_e/_s_r_c/_d_m_e_d_i_a/_s_o_u_n_d_c_o_m_m_a_n_d_s/_a_i_f_c_c_o_n_v_e_r_t._c.
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                                   - 4 -
  203.  
  204.  
  205.  
  206.        9.2  _C_h_a_n_g_e_s__a_n_d__A_d_d_i_t_i_o_n_s__S_i_n_c_e__6_._2
  207.  
  208.        This section lists changes/additions to the Audio File and
  209.        Audio Utility Library and accompanying example source code
  210.        since its last release.
  211.  
  212.           +o The AF Library now reads and writes Audio Visual
  213.             Research (avr), Amiga IFF/8SVX (iff), SampleVision
  214.             (smp), SoundFont2 (sf2), and VOC file formats in
  215.             addition to the existing set of formats.  Information
  216.             about these can be found in the following man pages:
  217.             _a_v_r(4), _i_f_f(4), _s_m_p(4), _s_f_2(4), and _v_o_c(4).
  218.  
  219.           +o Support for parsing of instruments, instrument
  220.             parameters, loops, and markers has been added for all
  221.             file formats which support some form of this
  222.             information.
  223.  
  224.           +o New routines for accessing loop counts and marker
  225.             comments are available.
  226.  
  227.           +o Four new compression types have been added:  CCITT
  228.             G.726, CCITT G.728, GSM 06.10, and IMA DVI ADPCM.
  229.  
  230.           +o The Audio File Library now has a _D_M_p_a_r_a_m_s-based
  231.             interface for audio data format parameters as well as
  232.             the old routines.
  233.  
  234.           +o As a result of the previous addition, it is now
  235.             possible to specify details of the format and/or rate
  236.             conversion process via _D_M_p_a_r_a_m_s.  This includes such
  237.             things as rate conversion and dithering algorithms.
  238.  
  239.           +o Error reporting within the AF is now handled by a
  240.             thread-safe error reporting routine at all times (even
  241.             when the AFerrorhandler has been disabled).
  242.             Applications that wish to take advantage of this
  243.             feature should call _d_m_G_e_t_E_r_r_o_r(3dm) in cases where an
  244.             AF routine returns an error status.
  245.  
  246.           +o The Audio Utility Library's functionality has been
  247.             included within the Digital Media Library,
  248.             libdmedia.so.  For backwards compatibility, a library
  249.             stub still exists for libaudioutil, but new
  250.             applications should use -_l_d_m_e_d_i_a rather than
  251.             -_l_a_u_d_i_o_u_t_i_l.
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.                                   - 5 -
  269.  
  270.  
  271.  
  272.        9.3  _C_h_a_n_g_e_s__a_n_d__A_d_d_i_t_i_o_n_s__S_i_n_c_e__b_e_t_w_e_e_n__5_._3__a_n_d__6_._2
  273.  
  274.        This section lists changes/additions to the Audio File
  275.        Library and accompanying example source code between the old
  276.        5.3 version and the newer 6.2 version.  This information
  277.        remains available for developers who are upgrading directly
  278.        from a 5.x release to 6.4.
  279.  
  280.           +o The 6.2 AF allows transparent conversion of the audio
  281.             data in a file's track into an uncompressed format of
  282.             the application's choice.  This new format is known as
  283.             the _v_i_r_t_u_a_l format of the data.  The application need
  284.             not concern itself with the data format in the file; it
  285.             can specify the format in which it would like to
  286.             receive the data.  This also works in the other
  287.             direction:  A buffer of data in any uncompressed format
  288.             may be written out to a file containing data in any
  289.             other format (assuming the audio file type specified
  290.             supports that format).  Read the _a_f_I_n_t_r_o(3dm) man page
  291.             for a detailed explanation of this procedure.
  292.  
  293.           +o The following list of audio utility programs has been
  294.             made obsolete by the new _d_m_c_o_n_v_e_r_t(3dm) and/or
  295.             _d_m_i_n_f_o(3dm) utilities:
  296.  
  297.             aaaaiiiiffffffff2222aaaaiiiiffffcccc,,,, aaaaiiiiffffcccc2222aaaaiiiiffffffff,,,, aaaaiiiiffffccccccccoooommmmpppprrrreeeessssssss,,,, aaaaiiiiffffccccddddeeeeccccoooommmmpppprrrreeeessssssss,,,, aaaaiiiiffffcccciiiinnnnffffoooo
  298.  
  299.             For backwards compatibility, these utilities still
  300.             exist as symbolic links to the new programs.  SGI
  301.             offers no guarantee that this will be continued in
  302.             future releases.
  303.  
  304.           +o The 6.x AF has adopted a new standard for the naming of
  305.             publically available routines.  All Audio File Library
  306.             routines now consist of the prefix "af" (in small case
  307.             letters) followed by any number of words describing the
  308.             function, with the first letter of each word
  309.             capitalized.  To allow existing code to run when linked
  310.             against the new library, two forms of backwards-
  311.             compatibility are built into the library:
  312.  
  313.                +o The public header file <dmedia/audiofile.h>
  314.                  contains a set of #define's which will cause the
  315.                  preprocessor to rename each occurrence of the old
  316.                  routine names in existing code when and if it is
  317.                  recompiled from source.
  318.  
  319.                +o For code which is not recompiled but which chooses
  320.                  to link against the 2.0 version of the library
  321.                  (see the section "How Does This DSO Magic Work?"
  322.                  below), a set of wrapper functions are contained
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.                                   - 6 -
  335.  
  336.  
  337.  
  338.                  within the library to allow the old routine symbol
  339.                  names to be resolved properly.  For ease in
  340.                  debugging and maximum efficiency, it is
  341.                  recommended that programs be recompiled to allow
  342.                  the redefines to happen, or better yet, rename the
  343.                  function calls using the new scheme.
  344.  
  345.        9.4  _B_u_g__F_i_x_e_s
  346.  
  347.        This section describes fixes made to the code since the 6.2
  348.        release.
  349.  
  350.           +o Audio format is now properly reset by _a_f_O_p_e_n_F_i_l_e in
  351.             cases where an illegal format was specified for
  352.             creation (ex., unsigned samples for AIFF file).
  353.  
  354.           +o Using an unmodified AFfilesetup to create an audio file
  355.             of any type now creates a file with default values
  356.             without generating any spurious warnings about
  357.             incorrect sample format, etc.
  358.  
  359.        9.5  _K_n_o_w_n__P_r_o_b_l_e_m_s__a_n_d__W_o_r_k_a_r_o_u_n_d_s
  360.  
  361.        This section lists problems in the new Audio File Library
  362.        software and ways to work around them.
  363.  
  364.           +o AIFF-C comment chunks: the Audio File Library API does
  365.             not yet include routines for reading/writing comment
  366.             chunk data in AIFF-C files.  The library routine
  367.             _a_f_O_p_e_n_F_i_l_e(3dm) allows you to open an AIFF-C file that
  368.             contains a comment chunk, but there is no way to access
  369.             the comment chunk fields through library routines.
  370.  
  371.           +o Apple Computer proprietary compression algorithms:
  372.             several compression algorithms described in the AIFF-C
  373.             specification (ACE2, ACE8, MAC3, MAC6) are Apple
  374.             proprietary and cannot be decoded on IRIS workstations.
  375.             To avoid problems with proprietary or unsupported
  376.             compression algorithms, convert AIFF-C files so that
  377.             they contain uncompressed data before attempting to
  378.             transfer them between an IRIS workstation and another
  379.             audio workstation platform.
  380.  
  381.           +o Aware MultiRate codec:  An application is only allowed
  382.             to have one AFfilehandle open at a time which uses the
  383.             Aware MultiRate or Lossless compressor or decompressor.
  384.             This applies to the application's entire process share
  385.             group.  In addition, the MultiRate algorithm has a
  386.             large number of memory leaks.  Avoid opening large
  387.             numbers of MultiRate-compressed files in any single
  388.             application.  Due to the limited support which SGI
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.                                   - 7 -
  401.  
  402.  
  403.  
  404.             supplies for this codec, it is unlikely that this will
  405.             be fixed in the foreseeable future.
  406.  
  407.             Note:  These bugs does not apply to the SGI MPEG1
  408.                    implementation.
  409.  
  410.        9.6  _D_o_c_u_m_e_n_t_a_t_i_o_n__E_r_r_o_r_s
  411.  
  412.        This section lists errors in the Audio File Library
  413.        documentation.
  414.  
  415.           +o No known errors at this time.
  416.  
  417.  
  418.        9.7  _A_u_d_i_o__F_i_l_e__L_i_b_r_a_r_y_:__D_e_v_e_l_o_p_e_r__N_o_t_e_s
  419.  
  420.        An important warning to developers about using the IRIX 6.2,
  421.        6.3 or 6.4 Audio File Library.  Read this in order to assure
  422.        that your applications continue to work well with the new
  423.        expanded version of the library.
  424.  
  425.        9.7.1  _I_n_t_r_o_d_u_c_t_i_o_n  It has come to our attention that some
  426.        applications which use the Audio File Library are making
  427.        untrue assumptions about the library.  These assumptions,
  428.        though in conflict with the practice laid out by our
  429.        documentation, happened coincidentally not to cause any
  430.        errors with versions of the library shipped with IRIX 5.3
  431.        and earlier.  But it is entirely possible that some of these
  432.        mis-coded applications will fail when run with this new
  433.        release of the library.
  434.  
  435.        As the Audio File Library is a Dynamic Shared Object (DSO),
  436.        this is a matter of some concern to developers, as end-users
  437.        may upgrade OS releases and get a new DSO which breaks their
  438.        applications without the applications' developer having had
  439.        a chance to re-release.
  440.  
  441.        The library released with IRIX 6.2 was a superset of the
  442.        previously-released library.  New functionality has been
  443.        added and will continue to be added, but all previously-
  444.        existing functions are still present.
  445.  
  446.        This section is provided to warn developers of problems
  447.        which may be experienced with the new releases due to
  448.        incorrect assumptions made in application code.
  449.  
  450.        The first problem involves assumptions about the kinds of
  451.        files the library will be able to open.  The second and
  452.        third problems have to do with using the Audio Library in
  453.        multi-threaded applications.  The fourth problem involves
  454.        the use of an AFfilehandle's file descriptor while the
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.                                   - 8 -
  467.  
  468.  
  469.  
  470.        filehandle is alive.  Then we follow with a warning about
  471.        the correlation between AF calls and actual UNIX operations
  472.        on files (such as read(), write(), and lseek() system
  473.        calls).
  474.  
  475.        9.7.2  _U_s_e__o_f__t_h_e__O_l_d__V_e_r_s_i_o_n__o_f__t_h_e__A_u_d_i_o__F_i_l_e__L_i_b_r_a_r_y
  476.        Because the confusion described above seems to be fairly
  477.        widespread, and because some of this confusion may have
  478.        stemmed from insufficient emphasis of these points in SGI's
  479.        documentation, the new library has been modified via the SGI
  480.        DSO versioning system so that programs built prior to the
  481.        6.2 release will still bind to a copy of the old library.
  482.        This means that old AF programs will not be able to get the
  483.        feature and performance improvements of the new Audio File
  484.        Library without relinking.  But this will also decrease the
  485.        chances that bugs in developers' application code such as
  486.        the ones described below will be exposed, until the
  487.        developer has a chance to repair their application, relink
  488.        to bind to the new DSO, and ship a new version to their
  489.        customers.
  490.  
  491.        We did this version change as a one-time aid for developers.
  492.        We do not intend to create new DSO versions for each new
  493.        audio file library upgrade.
  494.  
  495.        We give no guarantees that the old version of libaudiofile
  496.        will be maintained in the same manner as the new version, or
  497.        in any way whatsoever.  But we can guarantee that we will
  498.        not intentionally make changes to it which would cause
  499.        applications with the below pitfalls to fail.  Please note
  500.        however that some of the problems below could happen at any
  501.        time, whether libaudiofile changes or not.  Some could
  502.        surface to users as a result of a change of any software on
  503.        the system or even a change of system hardware.  So just
  504.        because we are keeping this older libaudiofile.so does not
  505.        mean developers have any guarantee whatsoever that their
  506.        programs will continue to execute fine.  All developers
  507.        should check their software for these problems and remove
  508.        them as soon as possible.
  509.  
  510.        9.7.3  _H_o_w__D_o_e_s__T_h_i_s__D_S_O__M_a_g_i_c__W_o_r_k_?  The version of the
  511.        Audio File library which has been shipping since IRIX 5.1 is
  512.        stamped with the interface version "sgi1.0".  This can be
  513.        seen by looking at the MIPS_IVERSION entry of the DSO using
  514.        "elfdump -L <library>".  Similarly, the new version of the
  515.        Audio File Library is stamped "sgi2.0".  When a program
  516.        (specifically, a NON-ABI executable) is linked under IRIX
  517.        5.x and the DSO is specified on the command line using an
  518.        argument such as -laudiofile, ld looks at the currently
  519.        available DSOs, picks the one called libaudiofile.so, and
  520.        places that DSO's name and its interface version stamp in
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.                                   - 9 -
  533.  
  534.  
  535.  
  536.        the executable's liblist, which can be seen with "elfdump
  537.        -Dl <executable>".
  538.  
  539.        Then at some later time, the executable is run.  This could
  540.        be before or after a new version of the Audio File Library
  541.        is installed.  The system (specifically, rld) must then find
  542.        a libaudiofile DSO to use which has a major version of 1
  543.        (the major version is the number before the decimal point in
  544.        the interface version stamp "sgi1.0").  If no new version of
  545.        the Audio File Library has been installed, the system will
  546.        immediately find libaudiofile.so, discover that it has an
  547.        interface version of "sgi1.0", and use it.  If a new version
  548.        of the Audio File Library has been installed as
  549.        libaudiofile.so, the system will find this DSO first, it
  550.        will look and see that the version number is "sgi2.0", and
  551.        it will reject this file.  It will then look for another DSO
  552.        called libaudiofile.so.1, since it wants to find major
  553.        version 1 of this DSO.  Since we have now installed the old
  554.        DSO libaudiofile.so.1, the system will find this DSO and use
  555.        it.
  556.  
  557.        In order for an old program to bind to the new DSO, the
  558.        programmer must relink it on a system which has version 2 of
  559.        the DSO.  This way, "sgi2.0" will be stored in the entry of
  560.        program's liblist for libaudiofile.so.  An alternate way to
  561.        make an AF program bind to the new DSO instead of the old
  562.        (1.0) DSO is to set the environment variable _RLD_ARGS to
  563.        '-ignore_version libaudiofile.so', but this will affect all
  564.        AF programs executed with this environment variable set.
  565.  
  566.        DSOs and DSO versioning are covered in dso(5), ld(1),
  567.        rld(1), and other man pages.  dso(5) contains references for
  568.        more information.
  569.  
  570.        9.7.4  _P_R_O_B_L_E_M__1  Assumption that AF only reads and writes a
  571.        certain set of file and data formats.
  572.  
  573.        The AF is a library which now supports many file formats
  574.        (besides AIFF and AIFF-C) and data formats (besides 2's
  575.        complement integer and compressed data formats).
  576.  
  577.        This is why we always stated in the old man pages that
  578.        certain formats were the "currently supported" formats (man
  579.        afIntro(3dm)), or that certain data formats or chunk
  580.        configurations were possible "from AIFF files" rather than
  581.        "from files".
  582.  
  583.        The Audio File Library has the following behaviors which may
  584.        break applications making the above assumption when a new
  585.        library is released:
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.                                   - 10 -
  599.  
  600.  
  601.  
  602.        int filefmt = afIdentifyFD(fd);
  603.        int filefmt = afGetFileFormat(handle, &version);
  604.  
  605.        Applications use afIdentifyFD for two reasons:  seeing
  606.        whether the AF recognizes an audio file, and seeing
  607.        specifically which format the audio file has.  The user uses
  608.        afGetFileFormat for the latter purpose (as well as getting
  609.        the format version, if applicable).  This section covers the
  610.        pitfalls of each use separately:
  611.  
  612.        CHECK FOR RECOGNITION:
  613.  
  614.        The filefmt returned could very well be something other than
  615.        AF_FILE_AIFF or AF_FILE_AIFFC.  This version of the library,
  616.        for example, supports six formats. The application should
  617.        make no assumptions about what tokens could be returned.
  618.  
  619.        Therefore code such as:
  620.  
  621.  
  622.            iiiiffff ((((ffffiiiilllleeeeffffmmmmtttt !!!!==== AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFF &&&&&&&&
  623.               ffffiiiilllleeeeffffmmmmtttt !!!!==== AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFFCCCC))))
  624.                   {{{{
  625.                   pppprrrriiiinnnnttttffff((((""""tttthhhhiiiissss ffffiiiilllleeee iiiissss nnnnooootttt ssssuuuuppppppppoooorrrrtttteeeedddd bbbbyyyy tttthhhheeee AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!\\\\nnnn""""))));;;;
  626.                   eeeexxxxiiiitttt((((0000))));;;;
  627.                   }}}}
  628.  
  629.        is incorrect because it is quite reasonable for the AF to
  630.        return a value other than one found in the include file.
  631.        Instead, the code should test whether the AF did not
  632.        recognize the file format rather than testing for what it
  633.        thinks is all possible recognized formats. Here's one
  634.        example:
  635.  
  636.  
  637.            iiiiffff ((((ffffiiiilllleeeeffffmmmmtttt ======== AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNSSSSUUUUPPPPPPPPOOOORRRRTTTTEEEEDDDD ||||||||
  638.                ffffiiiilllleeeeffffmmmmtttt ======== AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNKKKKNNNNOOOOWWWWNNNN))))
  639.                    {{{{
  640.                    pppprrrriiiinnnnttttffff((((""""tttthhhhiiiissss ffffiiiilllleeee iiiissss nnnnooootttt ssssuuuuppppppppoooorrrrtttteeeedddd bbbbyyyy tttthhhheeee AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!\\\\nnnn""""))));;;;
  641.                    eeeexxxxiiiitttt((((0000))));;;;
  642.                    }}}}
  643.  
  644.        The difference here is that the code acknowledges that there
  645.        are file formats which could be returned which the coder did
  646.        not know about at the time of coding.
  647.  
  648.        GETTING THE FORMAT ITSELF
  649.  
  650.        What about programs which actually care what the file format
  651.        of a file they are opening is?  Note that this should be
  652.        rare, because in the AF, the data format of a file's track
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                                   - 11 -
  665.  
  666.  
  667.  
  668.        is queried and manipulated totally independently of the
  669.        file's file format.  So only programs which actually have to
  670.        make distinctions like "AIFF vs. AIFF-C" need use
  671.        afGetFileFormat or afIdentifyFD for this purpose.
  672.  
  673.        The caveat here, just as stated above, is that the library
  674.        could easily return something other than one of the
  675.        AF_FILE_... tokens found in /usr/include/audiofile.h at the
  676.        time of compilation.  This is because the Audio File Library
  677.        is a dynamic shared object (DSO), and thus when newer
  678.        versions of the audio file library supporting new file
  679.        formats are released with new OS's, programs linked at an
  680.        earlier time will automatically pick up the new library, and
  681.        thus could see the tokens corresponding to the newly
  682.        supported formats.
  683.  
  684.        Before you start worrying about how to handle this
  685.        situation, make sure you have to worry about the file format
  686.        at all:
  687.  
  688.        Many applications which check the filefmt should really be
  689.        checking the sampfmt and other track parameters of the
  690.        file's track.  For example, there is no reason why a program
  691.        which simply reads the audio data out of a 16-bit
  692.        AF_SAMPFMT_TWOSCOMP AIFF file would not be able to function
  693.        just as well reading that kind of data out of a NeXT/Sun
  694.        file, as long as it did not intend to read AIFF-specific
  695.        chunks out of it as well (certain miscs and insts, for
  696.        example).  Such a program has no need to call afIdentifyFD
  697.        or afGetFileFormat to get the file format.
  698.  
  699.        But for those applications that do care about the file
  700.        format, code such as this:
  701.  
  702.  
  703.            sssswwwwiiiittttcccchhhh ((((aaaaffffIIIIddddeeeennnnttttiiiiffffyyyyFFFFDDDD((((ffffdddd))))))))
  704.            {{{{
  705.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFF::::
  706.                pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss iiiissss aaaannnn AAAAIIIIFFFFFFFF ffffiiiilllleeee....\\\\nnnn""""))));;;;
  707.                ddddoooo____aaaaiiiiffffffff____tttthhhhiiiinnnngggg(((())));;;;
  708.                bbbbrrrreeeeaaaakkkk;;;;
  709.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFFCCCC::::
  710.                    pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss iiiissss aaaannnn AAAAIIIIFFFFFFFF----CCCC ffffiiiilllleeee....\\\\nnnn""""))));;;;
  711.                    ddddoooo____aaaaiiiiffffffffcccc____tttthhhhiiiinnnngggg(((())));;;;
  712.                    bbbbrrrreeeeaaaakkkk;;;;
  713.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNKKKKNNNNOOOOWWWWNNNN::::
  714.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNSSSSUUUUPPPPPPPPOOOORRRRTTTTEEEEDDDD::::
  715.                    pppprrrriiiinnnnttttffff((((""""tttthhhhiiiissss ffffiiiilllleeee iiiissss nnnnooootttt ssssuuuuppppppppoooorrrrtttteeeedddd bbbbyyyy AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!!!!!\\\\nnnn""""))));;;;
  716.                    eeeexxxxiiiitttt((((0000))));;;;
  717.            ddddeeeeffffaaaauuuulllltttt::::
  718.                    ////**** aaaassssssssuuuummmmeeee tttthhhheeeerrrreeee hhhhaaaassss bbbbeeeeeeeennnn ssssoooommmmeeee pppprrrrooooggggrrrraaaammmmmmmmaaaattttiiiicccc eeeerrrrrrrroooorrrr ****////
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.                                   - 12 -
  731.  
  732.  
  733.  
  734.                    pppprrrriiiinnnnttttffff((((""""bbbbaaaadddd rrrreeeettttuuuurrrrnnnn vvvvaaaalllluuuueeee ffffrrrroooommmm AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!!!!!\\\\nnnn""""))));;;;
  735.                    aaaasssssssseeeerrrrtttt((((0000))));;;;
  736.            }}}}
  737.  
  738.        should be changed to code that at least does this:
  739.  
  740.  
  741.            sssswwwwiiiittttcccchhhh ((((aaaaffffIIIIddddeeeennnnttttiiiiffffyyyyFFFFDDDD((((ffffdddd))))))))
  742.            {{{{
  743.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFF::::
  744.                    pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss iiiissss aaaannnn AAAAIIIIFFFFFFFF ffffiiiilllleeee....\\\\nnnn""""))));;;;
  745.                    ddddoooo____aaaaiiiiffffffff____tttthhhhiiiinnnngggg(((())));;;;
  746.                    bbbbrrrreeeeaaaakkkk;;;;
  747.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____AAAAIIIIFFFFFFFFCCCC::::
  748.                    pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss iiiissss aaaannnn AAAAIIIIFFFFFFFF----CCCC ffffiiiilllleeee....\\\\nnnn""""))));;;;
  749.                    ddddoooo____aaaaiiiiffffffffcccc____tttthhhhiiiinnnngggg(((())));;;;
  750.                    bbbbrrrreeeeaaaakkkk;;;;
  751.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNKKKKNNNNOOOOWWWWNNNN::::
  752.            ccccaaaasssseeee AAAAFFFF____FFFFIIIILLLLEEEE____UUUUNNNNSSSSUUUUPPPPPPPPOOOORRRRTTTTEEEEDDDD::::
  753.                    pppprrrriiiinnnnttttffff((((""""tttthhhhiiiissss ffffiiiilllleeee iiiissss nnnnooootttt ssssuuuuppppppppoooorrrrtttteeeedddd bbbbyyyy AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!!!!!\\\\nnnn""""))));;;;
  754.                    eeeexxxxiiiitttt((((0000))));;;;
  755.            ddddeeeeffffaaaauuuulllltttt::::
  756.                    pppprrrriiiinnnnttttffff((((""""tttthhhhiiiissss pppprrrrooooggggrrrraaaammmm ccccaaaannnnnnnnooootttt hhhhaaaannnnddddlllleeee tttthhhhiiiissss ffffiiiilllleeee ffffoooorrrrmmmmaaaatttt!!!!\\\\nnnn""""))));;;;
  757.                    eeeexxxxiiiitttt((((0000))));;;;
  758.            }}}}
  759.  
  760.        Applications can now query the AF at runtime in order to
  761.        determine which file formats are supported by the version of
  762.        the AF to which the running program has bound.  In addition
  763.        to querying the token that is used to refer to each file
  764.        format, users can query for the textual name and other
  765.        characteristics of each format.  See the _a_f_Q_u_e_r_y(3dm) man
  766.        page for a complete description of this technique.
  767.  
  768.        For many applications, getting the textual name of the file
  769.        format is the entire reason they wanted to call afIdentifyFD
  770.        or afGetFileFormat in the first place, and so such
  771.        applications can be now written so that they instantly and
  772.        automatically understand every file format which the
  773.        currently-bound Audio File Library understands, as in:
  774.  
  775.  
  776.            iiiinnnntttt ffffiiiilllleeeeffffmmmmtttt ==== aaaaffffIIIIddddeeeennnnttttiiiiffffyyyyFFFFDDDD((((ffffdddd))));;;;
  777.            cccchhhhaaaarrrr ****nnnnaaaammmmeeee ==== ((((cccchhhhaaaarrrr ****)))) aaaaffffQQQQuuuueeeerrrryyyyPPPPooooiiiinnnntttteeeerrrr((((AAAAFFFF____QQQQUUUUEEEERRRRYYYYTTTTYYYYPPPPEEEE____FFFFIIIILLLLEEEEFFFFMMMMTTTT,,,,
  778.                                                 AAAAFFFF____QQQQUUUUEEEERRRRYYYY____NNNNAAAAMMMMEEEE,,,, ffffiiiilllleeeeffffmmmmtttt,,,, 0000,,,, 0000))));;;;
  779.            pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss iiiissss ffffiiiilllleeee hhhhaaaassss tttthhhheeee %%%%ssss ffffiiiilllleeee ffffoooorrrrmmmmaaaatttt....\\\\nnnn"""",,,, nnnnaaaammmmeeee))));;;;
  780.  
  781.        So what guarantees does the developer have?
  782.  
  783.        The library will never support fewer formats.  If a user
  784.        uses an AF_FILE_ token that we ship with a software release,
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.                                   - 13 -
  797.  
  798.  
  799.  
  800.        that token and its associated file format will never become
  801.        unsupported, although it may become obsolete (and perhaps
  802.        new features will not be available for it).  So for example,
  803.        the library will never stop supporting AF_FILE_AIFF or
  804.        AF_FILE_AIFFC.
  805.  
  806.        The library will always provide the query mechanism.  We
  807.        have made every effort to make the query mechanism versatile
  808.        enough that user programs can make intelligent decisions
  809.        about how to deal with and report info on audio files of
  810.        different file formats.
  811.  
  812.        afGetSampleFormat(AFfilehandle, int track, int *sampfmt, int
  813.        *sampwidth);
  814.  
  815.        Here is the single most likely offender that could be found
  816.        in developer's programs.  Because the new AF supports
  817.        transparent data format conversion, you may not have to use
  818.        this routine at all, and many of the below-mentioned issues
  819.        will go away.
  820.  
  821.        As the man page says, AIFF and AIFF-C files only support
  822.        AF_SAMPFMT_TWOSCOMP files.  However, as the library now
  823.        support other file formats, it is quite likely the library
  824.        may return some other sample format.  So code that says:
  825.  
  826.  
  827.            AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh ==== aaaaffffOOOOppppeeeennnnFFFFiiiilllleeee((((................))));;;;
  828.            iiiiffff ((((!!!!hhhh)))) rrrreeeettttuuuurrrrnnnn;;;;
  829.  
  830.            aaaaffffGGGGeeeettttSSSSaaaammmmpppplllleeeeFFFFoooorrrrmmmmaaaatttt((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, &&&&ssssaaaammmmppppffffmmmmtttt,,,, &&&&ssssaaaammmmppppwwwwiiiiddddtttthhhh))));;;;
  831.  
  832.            ////**** ssssaaaammmmppppffffmmmmtttt ccccoooouuuulllldddd oooonnnnllllyyyy bbbbeeee AAAAFFFF____SSSSAAAAMMMMPPPPFFFFMMMMTTTT____TTTTWWWWOOOOSSSSCCCCOOOOMMMMPPPP--------nnnnoooo nnnneeeeeeeedddd ttttoooo cccchhhheeeecccckkkk!!!! ****////
  833.  
  834.            sssswwwwiiiittttcccchhhh ((((ssssaaaammmmppppwwwwiiiiddddtttthhhh))))
  835.                    {{{{
  836.                    ............ ////**** sssseeeeeeee bbbbeeeelllloooowwww ffffoooorrrr nnnnooootttteeee oooonnnn tttthhhhiiiissss ppppaaaarrrrtttt ****////
  837.                    }}}}
  838.  
  839.        is incorrect.  It is by no means guaranteed that sampfmt is
  840.        two's complement.  Nor is it guaranteed that sampwidth has
  841.        any meaning--the meaning of sampwidth depends on sampfmt.
  842.  
  843.        For example, this version of the Audio File Library can
  844.        transfer IEEE floats and IEEE doubles.  For these types,
  845.        sampwidth has no meaning, and it is ignored and unset.  The
  846.        code above would behave very unpredictably.
  847.  
  848.        Code should say:
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.                                   - 14 -
  863.  
  864.  
  865.  
  866.            AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh ==== aaaaffffOOOOppppeeeennnnFFFFiiiilllleeee((((................))));;;;
  867.            iiiiffff ((((!!!!hhhh)))) rrrreeeettttuuuurrrrnnnn;;;;
  868.  
  869.            aaaaffffGGGGeeeettttSSSSaaaammmmpppplllleeeeFFFFoooorrrrmmmmaaaatttt((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, &&&&ssssaaaammmmppppffffmmmmtttt,,,, &&&&ssssaaaammmmppppwwwwiiiiddddtttthhhh))));;;;
  870.  
  871.            iiiiffff ((((ssssaaaammmmppppffffmmmmtttt !!!!==== AAAAFFFF____SSSSAAAAMMMMPPPPFFFFMMMMTTTT____TTTTWWWWOOOOSSSSCCCCOOOOMMMMPPPP))))
  872.                    {{{{
  873.                    pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss pppprrrrooooggggrrrraaaammmm ccccaaaannnn''''tttt rrrreeeeaaaadddd aaaauuuuddddiiiioooo ffffiiiilllleeeessss ooooffff tttthhhhiiiissss """"
  874.                           ssssaaaammmmpppplllleeee ffffoooorrrrmmmmaaaatttt\\\\nnnn""""))));;;;
  875.                    eeeexxxxiiiitttt((((0000))));;;;
  876.                    }}}}
  877.  
  878.            sssswwwwiiiittttcccchhhh ((((ssssaaaammmmppppwwwwiiiiddddtttthhhh)))) ////**** nnnnoooowwww wwwweeee kkkknnnnoooowwww ssssaaaammmmppppwwwwiiiiddddtttthhhh iiiissss mmmmeeeeaaaannnniiiinnnnggggffffuuuullll ****////
  879.                    {{{{
  880.                    ............ ////**** sssseeeeeeee bbbbeeeelllloooowwww ffffoooorrrr nnnnooootttteeee oooonnnn tttthhhhiiiissss ppppaaaarrrrtttt ****////
  881.                    }}}}
  882.  
  883.        Another note on sampwidth: even with AIFF files, the sample
  884.        width is not necessarily a multiple of 8.  Generally, this
  885.        can be ignored, because audio samples which do not take up
  886.        an integral number of bytes are left-justified inside the
  887.        next larger integral number of bytes (with the remaining
  888.        bits set to 0).  But this may be an issue in code such as
  889.        the switch statement above, if it were written:
  890.  
  891.  
  892.            sssswwwwiiiittttcccchhhh ((((ssssaaaammmmppppwwwwiiiiddddtttthhhh)))) ////**** nnnnoooowwww ssssaaaammmmppppwwwwiiiiddddtttthhhh iiiissss mmmmeeeeaaaannnniiiinnnnggggffffuuuullll ****////
  893.                    {{{{
  894.                    ccccaaaasssseeee 8888::::  ddddoooo____8888____tttthhhhiiiinnnngggg(((())));;;;  bbbbrrrreeeeaaaakkkk;;;;
  895.                    ccccaaaasssseeee 11116666:::: ddddoooo____11116666____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  896.                    ccccaaaasssseeee 22224444:::: ddddoooo____22224444____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  897.                    ccccaaaasssseeee 33332222:::: ddddoooo____33332222____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  898.                    ddddeeeeffffaaaauuuulllltttt::::
  899.                            ////**** ffffaaaallllsssseeee aaaassssssssuuuummmmppppttttiiiioooonnnn ****////
  900.                            pppprrrriiiinnnnttttffff((((""""bbbbaaaadddd rrrreeeettttuuuurrrrnnnn vvvvaaaalllluuuueeee ffffrrrroooommmm AAAAFFFF lllliiiibbbbrrrraaaarrrryyyy!!!!\\\\nnnn""""))));;;;
  901.                            aaaasssssssseeeerrrrtttt((((0000))));;;;
  902.                    }}}}
  903.  
  904.        There are two problems here.  One is that the code assumes
  905.        the maximum size of samples in files opened by the library
  906.        is 32 bits, which is definitely not true.  The second false
  907.        assumption is that the number of bits will be a multiple of
  908.        8.  Better code would be (after checking that sampfmt is an
  909.        integer format):
  910.  
  911.  
  912.            ////**** rrrroooouuuunnnndddd ssssaaaammmmppppwwwwiiiiddddtttthhhh uuuupppp ttttoooo nnnneeeeaaaarrrreeeesssstttt nnnnuuuummmmbbbbeeeerrrr ooooffff bbbbyyyytttteeeessss ****////
  913.            iiiinnnntttt nnnnbbbbyyyytttteeeessss ==== (((( ((((ssssaaaammmmppppwwwwiiiiddddtttthhhh----1111)))) //// 8888 )))) ++++ 1111;;;;
  914.            sssswwwwiiiittttcccchhhh ((((nnnnbbbbyyyytttteeeessss))))
  915.                    {{{{
  916.                    ccccaaaasssseeee 1111::::  ddddoooo____8888____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.                                   - 15 -
  929.  
  930.  
  931.  
  932.                    ccccaaaasssseeee 2222:::: ddddoooo____11116666____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  933.                    ccccaaaasssseeee 3333:::: ddddoooo____22224444____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  934.                    ccccaaaasssseeee 4444:::: ddddoooo____33332222____tttthhhhiiiinnnngggg(((())));;;; bbbbrrrreeeeaaaakkkk;;;;
  935.                    ddddeeeeffffaaaauuuulllltttt::::
  936.                            pppprrrriiiinnnnttttffff((((""""TTTThhhhiiiissss pppprrrrooooggggrrrraaaammmm ccccaaaannnn''''tttt rrrreeeeaaaadddd aaaauuuuddddiiiioooo ffffiiiilllleeeessss ooooffff tttthhhhiiiissss """"
  937.                                   ssssaaaammmmpppplllleeee wwwwiiiiddddtttthhhh %%%%dddd\\\\nnnn"""",,,, ssssaaaammmmppppwwwwiiiiddddtttthhhh))));;;;
  938.                            eeeexxxxiiiitttt((((0000))));;;;
  939.                    }}}}
  940.  
  941.        One final thing about sample widths--don't forget that there
  942.        is a special case for reading 24-bit integer data.  Because
  943.        3-byte data is so difficult to deal with, the library
  944.        automatically stretches the data out into convenient 4-byte
  945.        quantities in a way compatible with the SGI Audio Library
  946.        (AL).  The tricky thing here is that the data is sign-
  947.        extended on the left for this special case of 17-24-bit
  948.        data, whereas the padding that goes on to handle non-
  949.        multiple-of-8 sample widths is done by zero-bit-padding on
  950.        the right.  Here are some examples which should make this
  951.        clear, for the case of AF_SAMPFMT_TWOSCOMP:
  952.  
  953.  
  954.        MMMMSSSSBBBB                          LLLLSSSSBBBB
  955.        bbbbyyyytttteeee 3333  bbbbyyyytttteeee 2222  bbbbyyyytttteeee 1111  bbbbyyyytttteeee 0000
  956.        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddd |||| SSSSaaaammmmpppplllleeee FFFFoooorrrrmmmmaaaatttt
  957.        ------------------------------------------------------------------------------------------------------------------------------------++++--------------------------------------------------------------------------------------------------------------------------------
  958.                                dddddddddddddddddddddddd00000000 |||| 6666----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 8888 bbbbiiiittttssss
  959.                                      RRRRRRRR ||||  ((((rrrriiiigggghhhhtttt ppppaaaadddd))))
  960.                                         ||||
  961.                                dddddddddddddddddddddddddddddddd |||| 8888----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 8888 bbbbiiiittttssss
  962.                                         ||||  ((((nnnnoooo ppppaaaaddddddddiiiinnnngggg))))
  963.                                         ||||
  964.                        ccccccccccccccccccccccccccccccccdddddddddddddddd0000000000000000 |||| 11112222----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 11116666 bbbbiiiittttssss
  965.                                    RRRRRRRRRRRRRRRR ||||  ((((rrrriiiigggghhhhtttt ppppaaaadddd))))
  966.                                         ||||
  967.                        ccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddd |||| 11116666----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 11116666 bbbbiiiittttssss
  968.                                         ||||  ((((nnnnoooo ppppaaaaddddddddiiiinnnngggg))))
  969.                                         ||||
  970.        ssss<<<<------------------------bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccdddddddddddddddd0000000000000000 |||| 22220000----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 33332222 bbbbiiiittttssss
  971.        SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS                    RRRRRRRRRRRRRRRR ||||  ((((lllleeeefffftttt ssssiiiiggggnnnn----eeeexxxxtttteeeennnndddd AAAANNNNDDDD rrrriiiigggghhhhtttt ppppaaaadddd !!!!))))
  972.                                         ||||
  973.        ssss<<<<------------------------bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddd |||| 22224444----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 33332222 bbbbiiiittttssss
  974.        SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS                         ||||  ((((lllleeeefffftttt ssssiiiiggggnnnn----eeeexxxxtttteeeennnndddd))))
  975.                                         ||||
  976.        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccdddddddddddddddddddddddd00000000 |||| 33330000----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 33332222 bbbbiiiittttssss
  977.                                      RRRRRRRR ||||  ((((rrrriiiigggghhhhtttt ppppaaaadddd))))
  978.                                         ||||
  979.        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddd |||| 33332222----bbbbiiiitttt ddddaaaattttaaaa ttttaaaakkkkeeeessss 33332222 bbbbiiiittttssss
  980.                                         ||||  ((((nnnnoooo ppppaaaaddddddddiiiinnnngggg))))
  981.                                         ||||
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.                                   - 16 -
  995.  
  996.  
  997.  
  998.        Unlike file formats, it doesn't make sense to have a runtime
  999.        query of all possible sample formats.  The existence of the
  1000.        new virtual audio format system described above allows
  1001.        applications to specify in which format they wish to receive
  1002.        data.
  1003.  
  1004.        int afGetCompression(AFfilehandle, int);
  1005.        void afGetCompressionParams(AFfilehandle, int, int *, AUpvlist);
  1006.  
  1007.        Obviously, new compression methods will be supported, so the
  1008.        same warnings that apply to afGetSampleFormat apply here.
  1009.  
  1010.        As the library transparently decompresses input data and
  1011.        compresses output data, this is not such a big issue.  The
  1012.        application simply must specify the virtual format desired.
  1013.  
  1014.        A given already-released codec in our library will not
  1015.        change its native sample format, if we have specified what
  1016.        that codec's native sample format is.  So, for example, if
  1017.        you know the file is AF_COMPRESSION_G711_ULAW, then you can
  1018.        assume the native format for that codec is
  1019.        AF_SAMPFMT_TWOSCOMP, 16 because we said this in a man page.
  1020.        This is for backwards compatibility. But we do not guarantee
  1021.        that all future codecs will generate 16-bit signed integer
  1022.        data natively, nor do we guarantee that all future codecs
  1023.        will have a single native sample format (some may be
  1024.        configurable or may vary depending on what kind of data was
  1025.        originally compressed).  In every case though, the
  1026.        programmer can call afGetSampleFormat to retrieve the sample
  1027.        format for the current codec on the file corresponding to
  1028.        the given AFfilehandle.
  1029.  
  1030.        This new version of the library has a runtime query of all
  1031.        of the supported compression methods, their textual names,
  1032.        their compression ratios (if available), and any other info
  1033.        that may be useful in making a runtime choice of codec.
  1034.        This is done via afQuery(3dm) in a similar manner to the
  1035.        file format name string query above.
  1036.  
  1037.        int afGetChannels(AFfilehandle, int/*track*/);
  1038.  
  1039.        May return any number of channels, just like it could even
  1040.        with AIFF and AIFF-C.  The virtual number of channels may be
  1041.        set to any nonzero value.
  1042.  
  1043.        double afGetRate(AFfilehandle, int/*track*/);
  1044.  
  1045.        May return any sampling rate, just like it could even with
  1046.        AIFF and AIFF-C.  The current version of the library
  1047.        provides a transparent conversion mechanism which allows the
  1048.        programmer to deal with files with unexpected sampling rates
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.                                   - 17 -
  1061.  
  1062.  
  1063.  
  1064.        in a simple and clean way via virtual format conversion.
  1065.        See _a_f_S_e_t_V_i_r_t_u_a_l_R_a_t_e(3dm) for more information.
  1066.  
  1067.        int afGetTrackIDs(AFfilehandle, int */*track ids*/);
  1068.  
  1069.        May return more than one track.  We give no guarantees that
  1070.        only one-track formats are supported.  An application might
  1071.        want to reject such a file, or perhaps use the first track.
  1072.        We do not define the correct behavior.
  1073.  
  1074.        int afGetMarkIDs(AFfilehandle, int/*track*/, int */*ids*/);
  1075.        int afGetMarkPosition(AFfilehandle, int/*track*/, int/*markid*/);
  1076.        char *afGetMarkName(AFfilehandle, int, int);
  1077.  
  1078.        May return any configuration or number of marks just as it
  1079.        may with an AIFF/AIFF-C file.
  1080.  
  1081.        Apps should be written to expect and ignore marks they do
  1082.        not understand.
  1083.  
  1084.        int afGetMiscIDs(AFfilehandle, int * /*idlist*/);
  1085.        int afGetMiscType(AFfilehandle, int /*miscid*/);
  1086.        int afGetMiscSize(AFfilehandle, int /*miscid*/);
  1087.  
  1088.        May return ANY type of misc chunk.
  1089.  
  1090.        Apps should be written to expect and ignore miscellaneous
  1091.        chunks they do not understand.
  1092.  
  1093.        Apps should be especially careful not to copy misc chunks
  1094.        from one file to another unless they understand the content
  1095.        of those misc chunks;  the chunk may contain references to
  1096.        other parts of the file which the application has modified.
  1097.        In this case the chunk in the new file becomes corrupt.
  1098.        This is to be avoided.  The chunk should not be copied and
  1099.        the user should probably be warned.
  1100.  
  1101.        int afGetAESChannelData(AFfilehandle,int,unsigned char buf[24]);
  1102.  
  1103.        Will always return data that is to be interpreted as AES
  1104.        channel data, but there is never any guarantee that such
  1105.        data will be present or not in a given file format (unless
  1106.        that file format itself defines AES data as always being
  1107.        present).
  1108.  
  1109.        int afGetInstIDs(AFfilehandle, int */*inst ids*/);
  1110.        long afGetInstParamLong(AFfilehandle, int/*inst*/, int /*param*/);
  1111.  
  1112.        May return more than one inst, unlike the one fixed INST in
  1113.        AIFF/AIFF-C files.  There are many supported file formats
  1114.        which have different inst configurations than AIFF/AIFF-C.
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.                                   - 18 -
  1127.  
  1128.  
  1129.  
  1130.        The _a_f_Q_u_e_r_y mechanism allows an application to determine in
  1131.        advance the number of inst chunks supported by a particular
  1132.        file format.  Note that this is the allowable number for
  1133.        creating a file, not an indication of how many chunks are
  1134.        present in a file being read.  _a_f_G_e_t_I_n_s_t_I_D_s _M_U_S_T be used to
  1135.        determine the number present in a file opened for read
  1136.        access prior to loading the values into an array.  This is
  1137.        very important.  The following code will dump core when
  1138.        reading some of the currently supported file formats:
  1139.  
  1140.  
  1141.            AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh ==== aaaaffffOOOOppppeeeennnnFFFFiiiilllleeee((((................))));;;;
  1142.            iiiinnnntttt iiiinnnnssssttttIIIIDDDD;;;;  ////**** iiiinnnnccccoooorrrrrrrreeeecccctttt aaaassssssssuuuummmmppppttttiiiioooonnnn tttthhhhaaaatttt tttthhhheeeerrrreeee iiiissss oooonnnnllllyyyy oooonnnneeee iiiinnnnsssstttt IIIIDDDD!!!! ****////
  1143.            iiiiffff ((((!!!!hhhh)))) rrrreeeettttuuuurrrrnnnn;;;;
  1144.            aaaaffffGGGGeeeettttIIIInnnnssssttttIIIIDDDDssss((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, &&&&iiiinnnnssssttttIIIIDDDD))));;;; ////**** CCCCOOOORRRREEEE DDDDUUUUMMMMPPPP!!!!!!!! ****////
  1145.  
  1146.        The correct code should say:
  1147.  
  1148.  
  1149.            iiiinnnntttt ****iiiinnnnssssttttIIIIDDDDssss ==== NNNNUUUULLLLLLLL;;;;  ////**** ffffoooorrrr aaaarrrrrrrraaaayyyy ttttoooo----bbbbeeee----aaaallllllllooooccccaaaatttteeeedddd ****////
  1150.            iiiinnnntttt nnnnuuuummmmIIIIDDDDSSSS;;;;
  1151.            AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh ==== aaaaffffOOOOppppeeeennnnFFFFiiiilllleeee((((................))));;;;
  1152.            iiiiffff ((((!!!!hhhh)))) rrrreeeettttuuuurrrrnnnn;;;;
  1153.  
  1154.            nnnnuuuummmmIIIIDDDDssss ==== aaaaffffGGGGeeeettttIIIInnnnssssttttIIIIDDDDssss((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, NNNNUUUULLLLLLLL))));;;; ////**** cccchhhheeeecccckkkk nnnnuuuummmmbbbbeeeerrrr FFFFIIIIRRRRSSSSTTTT ****////
  1155.  
  1156.            iiiiffff((((nnnnuuuummmmIIIIDDDDssss >>>> 0000)))) {{{{
  1157.                iiiinnnnssssttttIIIIDDDDssss ==== ((((iiiinnnntttt ****)))) mmmmaaaalllllllloooocccc((((nnnnuuuummmmIIIIDDDDssss **** ssssiiiizzzzeeeeooooffff((((iiiinnnntttt))))))));;;; ////**** aaaallllllllooooccccaaaatttteeee aaaarrrrrrrraaaayyyy ****////
  1158.                iiiiffff((((!!!!iiiinnnnssssttttIIIIDDDDssss)))) {{{{
  1159.                    eeeerrrrrrrroooorrrr(((())));;;;
  1160.                    rrrreeeettttuuuurrrrnnnn;;;;
  1161.                }}}}
  1162.  
  1163.                aaaaffffGGGGeeeettttIIIInnnnssssttttIIIIDDDDssss((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, iiiinnnnssssttttIIIIDDDDssss))));;;; ////**** nnnnoooowwww llllooooaaaadddd IIIIDDDD aaaarrrrrrrraaaayyyy ****////
  1164.  
  1165.                ////**** ddddoooo wwwwhhhhaaaatttteeeevvvveeeerrrr wwwwiiiitttthhhh iiiinnnnssssttttIIIIDDDDssss ****////
  1166.  
  1167.                ffffrrrreeeeeeee((((iiiinnnnssssttttIIIIDDDDssss))));;;;  ////**** ffffrrrreeeeeeee tttthhhheeee mmmmeeeemmmmoooorrrryyyy ****////
  1168.            }}}}
  1169.  
  1170.        Apps should be written to expect and ignore instrument
  1171.        configurations or instrument parameters they do not
  1172.        understand.
  1173.  
  1174.        int afGetLoopIDs(AFfilehandle, int /*inst*/, int*);
  1175.        int afGetLoopMode(AFfilehandle, int/*inst*/, int /*loopid */);
  1176.        int afGetLoopStart(AFfilehandle, int/*inst*/, int/*loopid */);
  1177.        int afGetLoopEnd(AFfilehandle, int/*inst*/, int/*loopid*/);
  1178.        int afGetLoopTrack(AFfilehandle, int/*inst*/, int/*loopid*/);
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.                                   - 19 -
  1193.  
  1194.  
  1195.  
  1196.        May return any configurations of loops within an inst, not
  1197.        just the fixed value of 2 we have in AIFF/AIFF-C files.
  1198.        There are many supported file formats which have different
  1199.        loop configurations than AIFF/AIFF-C.
  1200.  
  1201.        The _a_f_Q_u_e_r_y mechanism allows an application to determine in
  1202.        advance the number of loops per inst supported by a
  1203.        particular file format. _a_f_G_e_t_L_o_o_p_I_D_s _M_U_S_T be used to
  1204.        determine the number present in a file opened for read
  1205.        access prior to loading the values into an array.  See the
  1206.        above note for afGetInstIDs for details.
  1207.  
  1208.        Apps should be written to expect and ignore loop
  1209.        configurations they do not understand.
  1210.  
  1211.        9.7.5  _P_R_O_B_L_E_M__2  Some developers are writing bad multi-
  1212.        threaded code that just happens to work with the AF library.
  1213.  
  1214.        We have caught several different apps trying to do the
  1215.        following from multi-threaded code and we imagine that there
  1216.        are other apps that do this as well:
  1217.  
  1218.  
  1219.        ================================ TTTThhhhrrrreeeeaaaadddd 1111 ========================================================||||============================================ TTTThhhhrrrreeeeaaaadddd 2222 ============================================
  1220.              ||||                         ||||          ||||
  1221.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1222.              ||||  nnnnoooo sssseeeemmmmaaaapppphhhhoooorrrreeee lllloooocccckkkkiiiinnnngggg   ||||          |||| nnnnoooo sssseeeemmmmaaaapppphhhhoooorrrreeee lllloooocccckkkkiiiinnnngggg
  1223.              ||||                         ||||          ||||
  1224.                                        ||||
  1225.        aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee1111))));;;;    ||||    aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee2222))));;;;
  1226.        aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;      ||||    aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;
  1227.                                        ||||
  1228.              ||||                         ||||          ||||
  1229.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1230.              ||||  nnnnoooo sssseeeemmmmaaaapppphhhhoooorrrreeee lllloooocccckkkkiiiinnnngggg   ||||          |||| nnnnoooo sssseeeemmmmaaaapppphhhhoooorrrreeee lllloooocccckkkkiiiinnnngggg
  1231.              ||||                         ||||          ||||
  1232.  
  1233.        Both threads are making calls on the same AFfilehandle at
  1234.        the same time, and no concern is put into the order in which
  1235.        these calls end up being made on the filehandle.
  1236.  
  1237.        This code is just plain old flat out wrong, it doesn't
  1238.        matter if you're using the AF or any other library.
  1239.  
  1240.        It is entirely possible that the order the calls get
  1241.        executed in by the UNIX scheduler is:
  1242.  
  1243.  
  1244.        aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee1111))));;;;    ||||
  1245.                                        ||||    aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee2222))));;;;
  1246.        aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;      ||||
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.                                   - 20 -
  1259.  
  1260.  
  1261.  
  1262.                                        ||||    aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;
  1263.  
  1264.        in which case both threads would read the wrong data. UNIX
  1265.        offers absolutely, positively no guarantee that this will
  1266.        not happen unless the programmer uses one of the many
  1267.        available process coordination facilities such as
  1268.        semaphores.
  1269.  
  1270.        Furthermore, no amount of work on the part of the AF library
  1271.        writers can prevent this situation, because the library has
  1272.        no knowledge of what order the programmer really wants the
  1273.        operations to come in.  We cannot use the pid as a hint
  1274.        because some correctly coded programs may want to seek in
  1275.        one thread and read in another (correctly coded programs use
  1276.        locking to make sure that these operations happen in the
  1277.        right order).
  1278.  
  1279.        Note that although our example only uses the functions
  1280.        afSeekFrame() and afReadFrames(), this problem can exist
  1281.        with calls to ANY Audio File Library functions, even ones
  1282.        which do not directly manipulate audio files (such as
  1283.        operations on an AFfilesetup or operations on an
  1284.        AFfilehandle that query or set parameters stored in memory).
  1285.        In particular, watch out for uncoordinated calls of
  1286.        afSyncFile() and afCloseFile().  Making these calls from
  1287.        more than one thread simultaneously in an uncoordinated
  1288.        fashion seems to be a common error.
  1289.  
  1290.        Correct code should look like this:
  1291.  
  1292.  
  1293.        ================================ TTTThhhhrrrreeeeaaaadddd 1111 ========================================================||||============================================ TTTThhhhrrrreeeeaaaadddd 2222 ============================================
  1294.                                        ||||
  1295.              ||||                         ||||          ||||
  1296.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1297.              ||||                         ||||          ||||
  1298.              ||||                         ||||          ||||
  1299.                                        ||||
  1300.        LLLLoooocccckkkk SSSSeeeemmmmaaaapppphhhhoooorrrreeee tttthhhhaaaatttt gggguuuuaaaarrrrddddssss hhhh    ||||    LLLLoooocccckkkk SSSSeeeemmmmaaaapppphhhhoooorrrreeee tttthhhhaaaatttt gggguuuuaaaarrrrddddssss hhhh
  1301.        aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee1111))));;;;    ||||    aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee2222))));;;;
  1302.        aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;      ||||    aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;
  1303.        UUUUnnnnlllloooocccckkkk SSSSeeeemmmmaaaapppphhhhoooorrrreeee tttthhhhaaaatttt gggguuuuaaaarrrrddddssss hhhh  ||||    UUUUnnnnlllloooocccckkkk sssseeeemmmmaaaapppphhhhoooorrrreeee tttthhhhaaaatttt gggguuuuaaaarrrrddddssss hhhh
  1304.                                        ||||
  1305.              ||||                         ||||          ||||
  1306.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1307.              ||||                         ||||          ||||
  1308.              ||||                         ||||          ||||
  1309.                                        ||||
  1310.  
  1311.        The key thing to realize here is: UNIX guarantees that only
  1312.        one of the Lock Semaphore calls will succeed immediately.
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.                                   - 21 -
  1325.  
  1326.  
  1327.  
  1328.        The thread whose lock does not succeed will sit and wait in
  1329.        the Lock Semaphore call (and thus not proceed to the
  1330.        afSeekFrame) until the other thread has unlocked the
  1331.        semaphore (after doing its seek AND its read).  Then when
  1332.        the other thread unlocks the semaphore, the thread who was
  1333.        forced to wait will now proceed.
  1334.  
  1335.        How does one use these semaphores?  One uses code such as
  1336.        the following to create a semaphore FileOpSema with value 1:
  1337.  
  1338.  
  1339.        ####iiiinnnncccclllluuuuddddeeee <<<<uuuulllloooocccckkkkssss....hhhh>>>>
  1340.  
  1341.        AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh;;;;      ////**** gggglllloooobbbbaaaallll ffffiiiilllleeee hhhhaaaannnnddddlllleeee ****////
  1342.        uuuusssseeeemmmmaaaa____tttt ****FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa;;;; ////**** gggglllloooobbbbaaaallll sssseeeemmmmaaaapppphhhhoooorrrreeee ttttoooo pppprrrrooootttteeeecccctttt hhhh ****////
  1343.  
  1344.        ////**** iiiinnnniiiittttiiiiaaaalllliiiizzzzeeee sssseeeemmmmaaaapppphhhhoooorrrreeee ssssuuuuppppppppoooorrrrtttt -------- ddddoooo tttthhhhiiiissss oooonnnncccceeee ****////
  1345.            {{{{
  1346.            uuuussssppppttttrrrr____tttt ****uuuussssppppttttrrrr;;;;
  1347.            cccchhhhaaaarrrr ****aaaarrrreeeennnnaaaaffffiiiilllleeee;;;;
  1348.  
  1349.            ////**** uuuusssseeee ffffaaaasssstttteeeesssstttt ((((nnnnoooo ddddeeeebbbbuuuuggggggggiiiinnnngggg)))) ffffoooorrrrmmmm ooooffff sssseeeemmmmaaaapppphhhhoooorrrreeeessss -------- uuuussssccccoooonnnnffffiiiigggg ((((3333PPPP)))) ****////
  1350.            uuuussssccccoooonnnnffffiiiigggg((((CCCCOOOONNNNFFFF____LLLLOOOOCCCCKKKKTTTTYYYYPPPPEEEE,,,, UUUUSSSS____NNNNOOOODDDDEEEEBBBBUUUUGGGG))));;;;
  1351.  
  1352.            ////**** ccccrrrreeeeaaaatttteeee sssshhhhaaaarrrreeeedddd aaaarrrreeeennnnaaaa ttttoooo hhhhoooolllldddd tttthhhheeee sssseeeemmmmaaaapppphhhhoooorrrreeee -------- uuuussssiiiinnnniiiitttt((((3333PPPP)))) ****////
  1353.            aaaarrrreeeennnnaaaaffffiiiilllleeee ==== ttttmmmmppppnnnnaaaammmm((((NNNNUUUULLLLLLLL))));;;;
  1354.            uuuussssppppttttrrrr ==== uuuussssiiiinnnniiiitttt((((aaaarrrreeeennnnaaaaffffiiiilllleeee))));;;;
  1355.  
  1356.            ////****
  1357.                ccccrrrreeeeaaaatttteeee tttthhhheeee sssseeeemmmmaaaapppphhhhoooorrrreeee iiiinnnn tttthhhhaaaatttt aaaarrrreeeennnnaaaa -------- uuuussssnnnneeeewwwwsssseeeemmmmaaaa((((3333PPPP))))
  1358.                ccccrrrreeeeaaaatttteeee wwwwiiiitttthhhh ccccoooouuuunnnntttt 1111--------tttthhhheeeerrrreeee iiiissss 1111 rrrreeeessssoooouuuurrrrcccceeee ((((hhhh)))),,,, wwwwhhhhiiiicccchhhh iiiissss
  1359.                iiiinnnniiiittttiiiiaaaallllllllyyyy aaaavvvvaaaaiiiillllaaaabbbblllleeee....
  1360.            ****////
  1361.            FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa ==== uuuussssnnnneeeewwwwsssseeeemmmmaaaa((((uuuussssppppttttrrrr,,,,1111))));;;;
  1362.  
  1363.            ////**** wwwweeee wwwwoooonnnn''''tttt nnnneeeeeeeedddd ttttoooo rrrreeeeffffeeeerrrr ttttoooo aaaarrrreeeennnnaaaa aaaaggggaaaaiiiinnnn ssssoooo wwwweeee ccccaaaannnn uuuunnnnlllliiiinnnnkkkk ffffiiiilllleeee ****////
  1364.            uuuunnnnlllliiiinnnnkkkk((((aaaarrrreeeennnnaaaaffffiiiilllleeee))));;;;
  1365.            }}}}
  1366.  
  1367.        Then one uses uspsema(3P) to lock the semaphore, and
  1368.        usvsema(3P) to unlock the semaphore.  Pretty simple:
  1369.  
  1370.  
  1371.        ================================ TTTThhhhrrrreeeeaaaadddd 1111 ========================================================||||============================================ TTTThhhhrrrreeeeaaaadddd 2222 ============================================
  1372.                                        ||||
  1373.              ||||                         ||||          ||||
  1374.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1375.              ||||                         ||||          ||||
  1376.              ||||                         ||||          ||||
  1377.                                        ||||
  1378.        uuuussssppppsssseeeemmmmaaaa((((FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa))));;;; ////****lllloooocccckkkk****////   ||||    uuuussssppppsssseeeemmmmaaaa((((FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa))));;;; ////****lllloooocccckkkk****////
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.                                   - 22 -
  1391.  
  1392.  
  1393.  
  1394.        aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee1111))));;;;    ||||    aaaaffffSSSSeeeeeeeekkkkFFFFrrrraaaammmmeeee((((hhhh,,,,ttttrrrraaaacccckkkk,,,,ppppllllaaaacccceeee2222))));;;;
  1395.        aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;      ||||    aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,,ttttrrrraaaacccckkkk,,,,............))));;;;
  1396.        uuuussssvvvvsssseeeemmmmaaaa((((FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa))));;;; ////****uuuunnnnlllloooocccckkkk****//// ||||    uuuussssvvvvsssseeeemmmmaaaa((((FFFFiiiilllleeeeOOOOppppSSSSeeeemmmmaaaa))));;;; ////****uuuunnnnlllloooocccckkkk****////
  1397.                                        ||||
  1398.              ||||                         ||||          ||||
  1399.              |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee     ||||          |||| ssssoooommmmeeee aaaammmmoooouuuunnnntttt ooooffff ttttiiiimmmmeeee
  1400.              ||||                         ||||          ||||
  1401.              ||||                         ||||          ||||
  1402.                                        ||||
  1403.  
  1404.        Now, why are we pointing this out for AF users?
  1405.  
  1406.        We have found several applications which were actually
  1407.        committing this error and were GETTING AWAY WITH IT!  By the
  1408.        most arcane of coincidences of CPU scheduler timings, these
  1409.        apps just happened to never fall into the short window of
  1410.        time in which the above worst-case scenario could occur.
  1411.  
  1412.        When these programs were recompiled with an alpha version of
  1413.        a newer Audio File Library, which contains different code
  1414.        and thus has slightly different timing characteristics and
  1415.        gets scheduled slightly differently, the bug in these
  1416.        applications was instantly revealed.
  1417.  
  1418.        This is why we want to alert developers to check their code
  1419.        for this-- there could be a bug of this type in their code
  1420.        which had previously not come out, just because the old AF
  1421.        happens to have had this property (by no design of our own).
  1422.        We want to give developers plenty of advanced warning about
  1423.        this situation.
  1424.  
  1425.        9.7.6  _P_R_O_B_L_E_M__3  Some developers are writing multi-threaded
  1426.        code that would be good under their assumption that the AF
  1427.        is MT/MP-safe. But this assumption is false.
  1428.  
  1429.        The AF is NOT a multi-thread and/or multi-processor safe
  1430.        library, in the following sense:
  1431.  
  1432.        Users can make multiple, simultaneous, uncoordinated AF
  1433.        calls on different AFfilehandles from different threads and
  1434.        the library will operate fine.  Each AFfilehandle completely
  1435.        encapsulates the state needed to do operations on that
  1436.        AFfilehandle (except for error handling, which is explained
  1437.        next).
  1438.  
  1439.        Users cannot make multiple, simultaneous, uncoordinated AF
  1440.        calls from different threads to set or access the library's
  1441.        global state--namely, the error handler function.  If two
  1442.        threads simultaneously try and set the error handler (even
  1443.        the same error handler), the behavior is undefined.
  1444.        Furthermore, if the user writes an error handler, then makes
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.                                   - 23 -
  1457.  
  1458.  
  1459.  
  1460.        multiple, simultaneous, uncoordinated AF calls on different
  1461.        filehandles from different threads, and both AF calls issue
  1462.        an error simultaneously, then two instances of the user's
  1463.        error handler will be called in a simultaneous,
  1464.        uncoordinated manner in two threads.  If this situation is
  1465.        possible in a user's program, the user should use semaphores
  1466.        in their error handler in order to make sure their handler
  1467.        doesn't try and report or deal with two errors at the same
  1468.        time.  Note that any AF function can cause an AF error to
  1469.        occur.  Do not assume a function will not err just because
  1470.        it is simple.
  1471.  
  1472.        A thread-safe error handling routine is now available via
  1473.        the Digital Media Library.  Any application which is
  1474.        expected to function in a multi-threaded fashion should
  1475.        disable the old global _A_F_e_r_r_o_r_h_a_n_d_l_e_r and use
  1476.        _d_m_G_e_t_E_r_r_o_r(3dm) in its place.  Here is a simple example:
  1477.  
  1478.  
  1479.            ####iiiinnnncccclllluuuuddddeeee <<<<ssssttttddddiiiioooo....hhhh>>>>
  1480.            ####iiiinnnncccclllluuuuddddeeee <<<<ddddmmmmeeeeddddiiiiaaaa////ddddmmmmeeeeddddiiiiaaaa....hhhh>>>>
  1481.            ####iiiinnnncccclllluuuuddddeeee <<<<ddddmmmmeeeeddddiiiiaaaa////aaaauuuuddddiiiiooooffffiiiilllleeee....hhhh>>>>
  1482.  
  1483.            ////**** tttthhhhiiiissss rrrroooouuuuttttiiiinnnneeee aaaassssssssuuuummmmeeeessss ffffiiiilllleeee hhhhaaaannnnddddlllleeee iiiissss ooooppppeeeennnn,,,, aaaannnndddd bbbbuuuuffffffffeeeerrrr iiiissss aaaallllllllooooccccaaaatttteeeedddd ****////
  1484.            ////**** rrrreeeettttuuuurrrrnnnnssss nnnnuuuummmmbbbbeeeerrrr ooooffff ffffrrrraaaammmmeeeessss aaaaccccttttuuuuaaaallllllllyyyy rrrreeeeaaaadddd,,,, oooorrrr ----1111 oooonnnn eeeerrrrrrrroooorrrr ****////
  1485.  
  1486.            iiiinnnntttt rrrreeeeaaaaddddBBBBuuuuffffffffeeeerrrr((((AAAAFFFFffffiiiilllleeeehhhhaaaannnnddddlllleeee hhhh,,,, vvvvooooiiiidddd ****bbbbuuuuffffffffeeeerrrr,,,, iiiinnnntttt ffffrrrraaaammmmeeeessss))))
  1487.            {{{{
  1488.                iiiinnnntttt ffffrrrraaaammmmeeeessssRRRReeeeaaaadddd ==== aaaaffffRRRReeeeaaaaddddFFFFrrrraaaammmmeeeessss((((hhhh,,,, AAAAFFFF____DDDDEEEEFFFFAAAAUUUULLLLTTTT____TTTTRRRRAAAACCCCKKKK,,,, bbbbuuuuffffffffeeeerrrr,,,, ffffrrrraaaammmmeeeessss))));;;;
  1489.                iiiiffff((((ffffrrrraaaammmmeeeessssRRRReeeeaaaadddd <<<< 0000)))) {{{{ ////**** eeeerrrrrrrroooorrrr!!!! ****////
  1490.                    ffffpppprrrriiiinnnnttttffff((((ssssttttddddeeeerrrrrrrr,,,, """"FFFFaaaattttaaaallll eeeerrrrrrrroooorrrr:::: %%%%ssss\\\\nnnn"""",,,, ddddmmmmGGGGeeeettttEEEErrrrrrrroooorrrr((((NNNNUUUULLLLLLLL,,,, NNNNUUUULLLLLLLL))))))));;;;
  1491.                    rrrreeeettttuuuurrrrnnnn ----1111;;;;
  1492.                }}}}
  1493.                rrrreeeettttuuuurrrrnnnn ffffrrrraaaammmmeeeessssRRRReeeeaaaadddd;;;;
  1494.            }}}}
  1495.  
  1496.        Then elsewhere, prior to this call, you would have:
  1497.  
  1498.  
  1499.            aaaaffffSSSSeeeettttEEEErrrrrrrroooorrrrHHHHaaaannnnddddlllleeeerrrr((((NNNNUUUULLLLLLLL))));;;;  ////**** ddddiiiissssaaaabbbblllleeee oooolllldddd----ssssttttyyyylllleeee gggglllloooobbbbaaaallll eeeerrrrrrrroooorrrr hhhhaaaannnnddddlllleeeerrrr ****////
  1500.  
  1501.        Now the most important caveat: users cannot make multiple,
  1502.        simultaneous, uncoordinated AF calls on the SAME
  1503.        AFfilehandle from different threads, even if the order of
  1504.        execution of those calls does not matter to the user.  Doing
  1505.        so will very likely cause a core dump, or at least
  1506.        corruption of the AFfilehandle.  This behavior will never be
  1507.        changed, as we refuse to make our developers pay the price
  1508.        of semaphore locking code at the beginning and end of every
  1509.        afReadFrames and afWriteFrames call.  Most users do not
  1510.        need, and in fact really do not want, semaphore protection
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.                                   - 24 -
  1523.  
  1524.  
  1525.  
  1526.        that is built-in to the AF calls themselves.
  1527.  
  1528.        Like problem 2, problem 3 can occur with any AF call, not
  1529.        just afSeekFrame() and afReadFrames().  In particular, watch
  1530.        out for simultaneous calls of afSyncFile() and
  1531.        afCloseFile().
  1532.  
  1533.        Note that at no point did we offer any guarantee that the
  1534.        library was MT/MP-safe, and in fact only libraries specified
  1535.        as such in man pages (such as the C library minus errno, as
  1536.        explained in intro(3)) are guaranteed to be so.
  1537.  
  1538.        9.7.7  _P_R_O_B_L_E_M__4  Some developers are using afOpenFD() or
  1539.        afGetFD() to get the file descriptor which an AFfilehandle
  1540.        is using, and then changing the file pointer on that file
  1541.        descriptor.  The AF does not allow this.
  1542.  
  1543.        The file descriptor returned by afGetFD() was intended to be
  1544.        used as part of a select() loop and was not intended to
  1545.        allow users to read, write, and seek in the file without the
  1546.        knowledge of the Audio File Library.  Doing so will cause
  1547.        the library to give unpredictable results unless the user
  1548.        saves and restores the file position whenever they modify
  1549.        it.
  1550.  
  1551.        The reason for this is that we are not willing to force upon
  1552.        users expecting high performance the often very large
  1553.        overhead of an lseek(2) system call on every afReadFrames()
  1554.        or afWriteFrames().  Therefore we cannot restore the file
  1555.        position to a known place on every AF call which relies on
  1556.        the file being in that place.
  1557.  
  1558.        Users who wish to modify the file position of the file
  1559.        descriptor given by afGetFD() should save the file position
  1560.        and restore it after they are finished and before they make
  1561.        the next AF call to read or write data to the file.  The new
  1562.        routines _a_f_S_a_v_e_F_i_l_e_P_o_s_i_t_i_o_n(3dm) and
  1563.        _a_f_R_e_s_t_o_r_e_F_i_l_e_P_o_s_i_t_i_o_n(3dm) have been created expressly for
  1564.        this purpose.  Alternately they can use one of two different
  1565.        file descriptors opened to the same file (dup(2) will not
  1566.        work; it gives you two file descriptors which share a file
  1567.        offset.  The file must be re-opened in order to get a
  1568.        separate file descriptor).  Also, if users attempt to write
  1569.        to the file, no matter how the AFfilehandle was opened, the
  1570.        results are undefined.
  1571.  
  1572.        Developers had asked for a call to get the offset of the
  1573.        audio data in an audio file.  Two new functions have been
  1574.        made available.  afGetDataOffset(3dm) returns the offset in
  1575.        bytes from the beginning of an open audio file to the first
  1576.        audio sample.  afGetTrackBytes(3dm) returns the size in
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.                                   - 25 -
  1589.  
  1590.  
  1591.  
  1592.        bytes of the audio data portion of a given track in an audio
  1593.        file.
  1594.  
  1595.        9.7.8  _W_A_R_N_I_N_G  We give no guarantees about the number or
  1596.        nature of UNIX system calls that will result from a given AF
  1597.        call.  In particular, afReadFrames() and afWriteFrames()
  1598.        could actually read() or write() any amount of data in the
  1599.        file, or could read() or write() more than once in varying
  1600.        chunk sizes.  Also, afOpenFile(), afSeekFrame(),
  1601.        afSyncFile(), afCloseFile(), and other AF functions could
  1602.        result in any amount of data being read from or written to
  1603.        the file.  (but of course the AF will not write to a file
  1604.        opened for read access or read from a file opened for write
  1605.        access).
  1606.  
  1607.        Users who are attempting to optimize the I/O in their
  1608.        program by managing I/O system call behavior should be aware
  1609.        that at this time we offer no guarantees about when the AF
  1610.        will perform system calls.
  1611.  
  1612.        This has not been a problem to our knowledge, but we thought
  1613.        it would be a good idea just to make this explicit.  At this
  1614.        time we offer no guarantees that a read of, say, 50,000
  1615.        frames will result in a single UNIX read() system call for
  1616.        exactly 50,000 frames worth of data.  Although we will
  1617.        strive to make this the case whenever it seems beneficial,
  1618.        there are certain cases where this is simply impossible.
  1619.        For example, reads from or writes to a compressed file will
  1620.        generally be split up into a chunksize which is natural for
  1621.        the codec being used, and thus possibly result in several
  1622.        reads or writes to the file.  Also, we give no guarantee
  1623.        that afSeekFrame() will seek the file at all in the UNIX
  1624.        lseek() sense, although it happens that this is generally
  1625.        the case for the current Audio File Library.  And even if
  1626.        afSeekFrame() does lseek() the file, we do not guarantee
  1627.        that it will be to the exact frame the user specified
  1628.        (though this is generally the case in the old AF library).
  1629.        This is due to necessary compensation for filter delays
  1630.        inherent in certain forms of compression and decompression.
  1631.  
  1632.        In a future release, features will be introduced which may
  1633.        make this more of an issue.  At that time we will come up
  1634.        with much more specific information about what guarantees we
  1635.        can offer to the developer.  We want to find out if these
  1636.        kinds of guarantees are something that developers need at
  1637.        all.  Perhaps it is simply not an issue for developers.  But
  1638.        if it is, we would be most interested in hearing your
  1639.        feedback.  You can contact your local SGI representative and
  1640.        ask them to send a message to the developers of the Audio
  1641.        File Library, or you can also use the newsgroup
  1642.        comp.sys.sgi.audio for faster and much more direct contact.
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.                                   - 26 -
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662.  
  1663.  
  1664.  
  1665.  
  1666.  
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673.  
  1674.  
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690.  
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.  
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.